package omq.panel;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.prefs.Preferences;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import omq.api.OMQConnectionFactory;
import omq.api.OMQConnectionSOAP;
import omq.api.OMQMessage;
import omq.api.OMQQueueManager;
import omq.api.logger.OMQLogger;
import omq.screen.OmqTesterMainScreen;

import component.OmqButton;

import constant.OmqTesterClientConst;

public class DeleteMsgPanel extends OmqPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1660408658206777551L;

	private JLabel responseLbl;
	private OmqButton delBtn;
	private JLabel infoLbl;
	private JTextField msgIdInput;
	
	public DeleteMsgPanel() {
		super();
		initialize();
	}

	public DeleteMsgPanel(boolean arg0) {
		super(arg0);
		initialize();
	}

	public DeleteMsgPanel(LayoutManager arg0, boolean arg1) {
		super(arg0, arg1);
		initialize();
	}

	public DeleteMsgPanel(LayoutManager arg0) {
		super(arg0);
		initialize();
	}
	
	private void initialize(){
		
		
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.anchor = GridBagConstraints.PAGE_START;
		c.gridx = 0;
		
		c.gridy = 0;
		add(getInfoLbl(),c);
		
		c.gridy = 1;
		c.insets = new Insets(10,0,0,0);
		OmqPanel btnPnl = new OmqPanel(new FlowLayout());
		btnPnl.add(getMsgIdInput());
		btnPnl.add(getDelBtn());
		btnPnl.add(new JLabel(""));
		add(btnPnl,c);

		c.gridy = 2;
		add(getResponseLbl(),c);
		
		c.gridy = 3;
		c.weighty = 1.0;
		add(new JLabel(""),c);
		initpref();
	}

	
	public JLabel getInfoLbl() {
		if(infoLbl == null){
			infoLbl = new JLabel("<html><body>" + getTitle("Delete (DEL) message") +" Well, you can delete a message Just by typing its ID and press the 'Delete' button.<br></body></html>");
		}
		return infoLbl;
	}

	public JLabel getResponseLbl() {
		if(responseLbl == null){
			responseLbl = new JLabel("");
		}
		return responseLbl;
	}

	public OmqButton getDelBtn() {
		if(delBtn == null){
			delBtn = new OmqButton("Delete");
			delBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {

					HashMap pref = OmqTesterMainScreen.prefMap;
					 JOptionPane.showMessageDialog(new JFrame(),"<html><body>Deleting message, please wait ...</body></html>","Message",JOptionPane.INFORMATION_MESSAGE);
					 waitForInfo(true);
					 pref.put("msg_id",getMsgIdInput().getText());
					 OMQMessage msg = null;
					try {
						
						conn = OMQConnectionFactory.getConnection(myPreferences.get(OmqTesterClientConst.PREF_USER_NAME, OmqTesterClientConst.PREF_USER_NAME),
								myPreferences.get(OmqTesterClientConst.PREF_PASSWORD, OmqTesterClientConst.PREF_PASSWORD),
								"http://mq.onlinemq.com/webservice_soap_v1/api",
								new OMQQueueManager(myPreferences.get(OmqTesterClientConst.PREF_QUEUE_MGR, OmqTesterClientConst.PREF_QUEUE_MGR)));

						OMQLogger.getInstance().info("user = "+myPreferences.get(OmqTesterClientConst.PREF_USER_NAME, OmqTesterClientConst.PREF_USER_NAME));						
						OMQLogger.getInstance().info("qmgr = "+myPreferences.get(OmqTesterClientConst.PREF_QUEUE_MGR,OmqTesterClientConst.PREF_QUEUE_MGR));						
						OMQLogger.getInstance().info("q    = "+myPreferences.get(OmqTesterClientConst.PREF_QUEUE_NAME,OmqTesterClientConst.PREF_QUEUE_NAME));						

						sess = conn.createSession(false);
						
						// TODO FIX DELETE MESSAGE
//						msg = connection.delete_message(pref);
						
					} catch (Exception e) {
						OMQLogger.getInstance().error(e.getMessage());
						JOptionPane.showMessageDialog(new JFrame(),"<html><body>" + e.getMessage() + "</body></html>","System error",JOptionPane.ERROR_MESSAGE);
						waitForInfo(false);
					}
					 String response = "<html><body><h2>Delete message</h2><bR>";
					 if(sess.getLastErrorCode() == 0){
						 OMQLogger.getInstance().debug("Deleted the message");
						 response += " The message , " + msg.getMsgID() + ", Has been deleted from the queue : " + myPreferences.get(OmqTesterClientConst.PREF_QUEUE_MGR,OmqTesterClientConst.PREF_QUEUE_MGR) + "." + myPreferences.get(OmqTesterClientConst.PREF_QUEUE_NAME,OmqTesterClientConst.PREF_QUEUE_NAME) + "</body></html>";
					 }else{
						 response = printErrorFromOMQ("Delete message", true);
					 }
					 
					 waitForInfo(false);
					 getResponseLbl().setText(response);
				}
			});
		}
		return delBtn;
	}

	public JTextField getMsgIdInput() {
		if(msgIdInput == null){
			msgIdInput = new JTextField("-- Insert message ID here --");
			msgIdInput.setPreferredSize(new Dimension(180,25));
		}
		return msgIdInput;
	}
	
	public void resetPanel(){
		getMsgIdInput().setText("-- Insert message ID here --");
		getResponseLbl().setText("");
	}

}
